import 'package:star_author/utils/utils.dart';
import 'package:star_author/views/products/bidding_product_list_item.dart';
import 'package:flutter/material.dart';
import 'package:loading_more_list/loading_more_list.dart';

class ProductListWidget extends StatelessWidget {
  bool _isSliver = false;
  late int? itemCount;
  late List list;
  late BiddingProductListItemStyle style;
  late Widget? lastIndicator;
  late ScrollController? controller;
  late Function? callBack;
  BiddingProductListItemFormType detail;
  double spacing = 10;
  final Function refreshPage;

  ProductListWidget.normal(
      {Key? key,
      this.itemCount,
      this.lastIndicator,
      this.controller,
      required this.list,
      this.callBack,
      this.style: BiddingProductListItemStyle.Inside,
      required this.refreshPage,
      this.detail: BiddingProductListItemFormType.BiddingDetail})
      : super(key: key);

  ProductListWidget.sliver({
    Key? key,
    this.itemCount,
    required this.list,
    this.callBack,
    this.style: BiddingProductListItemStyle.Inside,
    required this.refreshPage,
    this.detail: BiddingProductListItemFormType.BiddingDetail,
  })  : _isSliver = true,
        super(key: key);

  int? _getCount() {
    if (itemCount != null) {
      return itemCount;
    } else if (lastIndicator != null) {
      return list.length + 1;
    }
  }

  double minPrice = 1000; //最小值

  @override
  Widget build(BuildContext context) {
    Widget _widget;
    if (!_isSliver) {
      _widget = LayoutBuilder(
          builder: (BuildContext ctx, BoxConstraints constraints) {
        double padding =
            MediaQuery.of(context).size.width == constraints.maxWidth ? 12 : 0;
        double width = (constraints.maxWidth - padding * 2 + 10) / 2;
        return Padding(
          padding: GetPadding.only(
            left: padding,
            right: padding,
          ),
          child: WaterfallFlow.builder(
            controller: controller,
            addAutomaticKeepAlives: false,
            addRepaintBoundaries: false,
            shrinkWrap: itemCount != null,
            padding: EdgeInsets.zero,
            //不知道为什么会有自带的间距 需要清除
            physics: itemCount != null
                ? NeverScrollableScrollPhysics()
                : ClampingScrollPhysics(),
            gridDelegate: SliverWaterfallFlowDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: spacing,
              crossAxisSpacing: spacing,
              lastChildLayoutTypeBuilder: (index) {
                if (index == list.length) {
                  return LastChildLayoutType.foot;
                } else {
                  return LastChildLayoutType.none;
                }
              },
            ),
            itemBuilder: (context, index) {
              if (index < list.length) {
                if (style == BiddingProductListItemStyle.Outside) {
                  if (list[index]['confirmed_price'] < minPrice) {
                    return Container();
                  }
                }
                return Padding(
                  padding: GetPadding.only(top: index <= 1 ? 12 : 0),
                  child: BiddingProductListItem(
                    list[index],
                    itemStyle: style,
                    refreshPage: refreshPage,
                    formDetail: detail,
                    callback: callBack,
                    width: width,
                  ),
                );
              } else {
                return lastIndicator ?? Container();
              }
            },
            itemCount: _getCount(),
          ),
        );
      });
    } else {
      _widget = SliverLayoutBuilder(
        builder: (context, constraints) {
          double padding =
              MediaQuery.of(context).size.width == constraints.crossAxisExtent
                  ? 12
                  : 0;
          double width = (constraints.crossAxisExtent - padding * 2 + 10) / 2;
          return SliverPadding(
            padding: GetPadding.only(
              left: padding,
              right: padding,
            ),
            sliver: SliverWaterfallFlow(
              delegate: SliverChildBuilderDelegate(
                (BuildContext c, int index) {
                  if (style == BiddingProductListItemStyle.Outside) {
                    //小于100的不显示
                    if (double.tryParse(
                                list[index]['confirmed_price'].toString())!
                            .toDouble() <
                        minPrice) {
                      return Container();
                    }
                  }
                  return Padding(
                    padding: GetPadding.only(top: index <= 1 ? 12 : 0),
                    child: BiddingProductListItem(
                      list[index],
                      itemStyle: style,
                      refreshPage: refreshPage,
                      formDetail: detail,
                      callback: callBack,
                      width: width,
                    ),
                  );
                },
                childCount: itemCount,
                addAutomaticKeepAlives: false,
                addRepaintBoundaries: false,
              ),
              gridDelegate: SliverWaterfallFlowDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                mainAxisSpacing: spacing,
                crossAxisSpacing: spacing,
              ),
            ),
          );
        },
      );
    }
    if (!_isSliver) {
      _widget = Container(
        width: double.infinity,
        child: _widget,
      );
    }
    return _widget;
  }
}
